'Tissue Initial Review Requirement Script  
Option Strict Off
imports System
imports System.Collections
imports PHSRAG.Insight21.Humans
imports PHSRAG.Insight21.BusinessObjects
imports FormsManagement = PHSRAG.Insight21.FormsManagement

module Script
sub Main()

    Dim speciesAnswers as ArrayList = questAnswer.GetAnswers("Species")
    theProtocol.speciesCode = speciesAnswers(0).Value

    Dim hazardExists as Boolean
    for each qa as FormsManagement.QuestionnaireAnswer in currentProtVersion.QuestionnaireAnswers
        if qa.QuestionnaireCategory = "QUESTCAT_ANM_HZ" then
            hazardExists = true
        end if
    next

    Dim answerNameValuePairs as new ArrayList
    answerNameValuePairs.Add( "Species" )
    answerNameValuePairs.Add( "MONKEYS_BABOON" )
    answerNameValuePairs.Add( "Species" )
    answerNameValuePairs.Add( "MONKEYS_MACAQUE_CYNO" )
    answerNameValuePairs.Add( "Species" )
    answerNameValuePairs.Add( "MONKEYS_MACAQUE_RHESUS" )
    answerNameValuePairs.Add( "Species" )
    answerNameValuePairs.Add( "MONKEYS_SQUIRREL" )

    AddForm( answerNameValuePairs, "Hazardous Agent Administration and Use", hazardExists )

    theProtocol.Save( connectionString )
    currentProtVersion.Save ( connectionString )
    currentProcess.Save( connectionString )
end sub  

' Adds or removes questionnaire answers to protocol version based on answer to a question
sub AddForm(byval answerName as string, byval answerValue as string, byval questionnaireName as string, byval formExists as boolean)
    Dim answers as Arraylist = questAnswer.GetAnswers( answerName )
    if answers.Count > 0 then
        if answers(0).Value = answerValue and not formExists then
            Dim qv as FormsManagement.QuestionnaireVersion = FormsManagement.Questionnaire.GetActiveVersion( cachePolicy, questionnaireName, FormsManagement.Constants.QuestionnaireType.Animals )
            Dim qa as FormsManagement.QuestionnaireAnswer = currentProtVersion.AddNewQuestionnaireAnswer(qv.Id, cachePolicy, questionnaireName, not qv.Questionnaire.AllowMultiple)
        else if answers(0).Value <> answerValue then
            currentProtVersion.DeleteQuestionnaireAnswer(questionnaireName)
        end if 
    else
        currentProtVersion.DeleteQuestionnaireAnswer(questionnaireName)
    end if
end sub

' Adds or removes questionnaire answers to protocol version based on answer to a question
' This overload takes mutliple questions / answers to determine if the QA needs to be added or 
' removed.
sub AddForm(byval answerNameValuePairs as ArrayList, byval questionnaireName as string, byval formExists as boolean)
    Dim add as boolean = false
    Dim i as integer
    for i = 0 to answerNameValuePairs.Count - 1 step 2
        Dim answers as Arraylist = questAnswer.GetAnswers( answerNameValuePairs(i) )
        if answers.Count > 0 then
            if answers(0).Value = answerNameValuePairs(i+1) then
                add = true
            end if
        end if
    next
    if add and not formExists then
        Dim qv as FormsManagement.QuestionnaireVersion = FormsManagement.Questionnaire.GetActiveVersion( cachePolicy, questionnaireName, FormsManagement.Constants.QuestionnaireType.Animals )
        Dim qa as FormsManagement.QuestionnaireAnswer = currentProtVersion.AddNewQuestionnaireAnswer(qv.Id, cachePolicy, questionnaireName, not qv.Questionnaire.AllowMultiple)
    else if not add then
        currentProtVersion.DeleteQuestionnaireAnswer(questionnaireName)
    end if
end sub

end module
